package pers.sen.web;

import javax.servlet.AsyncContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author Jason
 * @version 1.0.0
 * @date 2020/12/11 16:27
 * @Description TODO
 */
@WebServlet(value = "/syncServlet",loadOnStartup = 5)
public class SyncServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        long start = System.currentTimeMillis();
        // 模拟执行业务
        doSomething(resp);
        // 打印耗时时间
        System.out.println("同步耗时: " + (System.currentTimeMillis() - start));
    }

    private void doSomething(HttpServletResponse response) {
        try {
            // 模拟延迟
            Thread.sleep(3000);
            // 模拟业务
            response.getWriter().write("sync, hello !");
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}
